﻿/*
* Copyright (c) 2011, Johnson County Community College (JCCC)
* All rights reserved.
* BSD License: http://www.opensource.org/licenses/bsd-license.php
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
*      in the documentation and/or other materials provided with the distribution.
*    * Neither the name of JCCC nor the names of its contributors may be used to endorse or promote products derived 
*      from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;



public class DungeonRoom
{
    public Rectangle area;
    public Rectangle paddedArea;
    public int left;
    public int right;
    public int top;
    public int bottom;
    List<int> borderIndex;

    public void setRoom(Rectangle area, int padding)
    {
        this.area = area;
        this.paddedArea.X = area.X - padding;
        this.paddedArea.Y = area.Y - padding;
        this.paddedArea.Width = area.Width + padding;
        this.paddedArea.Height = area.Height + padding;
        left = area.X;
        right = area.X + area.Width;
        top = area.Y;
        bottom = area.Y + area.Height;

        borderIndex = new List<int>();

           
    }

    public int[] GetBorderIndex()
    {
        return borderIndex.ToArray(); 
    }

    public void GenerateBorderIndex(int mapWidth, int mapHeight)
    {
        int max = mapWidth * mapHeight;

        borderIndex.Clear();

        int start, stop;
        int startX, stopX;
        int startY, stopY;
        //top first            
        //if against border, don't do top
        if (top > 0)
        {
            startX = left - 1;
            if (startX < 0) startX = 0;
            startY = top - 1;
            start = startY * mapWidth + startX;

            stopX = right;
            if (stopX >= mapWidth) stopX = mapWidth - 1;
            stopY = startY;
            stop = stopY * mapWidth + stopX;
                
            for (int i = start; i <= stop; i++) borderIndex.Add(i);
        }
            
        //bottom next
        //if on border don't do
        if (bottom < mapHeight)
        {
            startX = left - 1;
            if (startX < 0) startX = 0;
            startY = bottom;
            start = startY * mapWidth + startX;

            stopX = right;
            if (stopX >= mapWidth) stopX = mapWidth - 1;
            stopY = startY;
            stop = stopY * mapWidth + stopX;

            for (int i = start; i <= stop; i++) borderIndex.Add(i);
        }

        //now left
        //standard don't do if on edge
        if (left > 0)
        {

            startX = left - 1;
            startY = top;
            start = startY * mapWidth + startX;

            stopX = startX;
            stopY = bottom - 1;
            stop = stopY * mapWidth + stopX;

            for (int i = start; i <= stop; i += mapWidth) borderIndex.Add(i);
        }

        //and finally right
        //of course don't do if on edge
        if (right < mapWidth)
        {
            startX = right;
            startY = top;
            start = startY * mapWidth + startX;

            stopX = startX;
            stopY = bottom - 1;
            stop = stopY * mapWidth + stopX;

            for (int i = start; i <= stop; i += mapWidth) borderIndex.Add(i);
        }
    }
}

